require "Common/define"
require "Logic/Config"
require "Common/functions"

local TaskItem = {};
HelperFunc.SetUIEnv(TaskItem)
local this = TaskItem;

this.taskList = nil;
this.isClick = false;
this.challengeTimeCountDown = 1;
this.activityId = 19001;
local SPECIAL_RECHARGE_TYPE = 4
function TaskItem.initData(id, obj, timeCountDown, taskType)
    ---@type GList
    this.activityId = tonumber(id);
    this.ranklist = obj:GetChild("List");
    this.ranklist.itemRenderer = this.updateItem;
    this.ranklist:SetVirtual();

    this.updateData(timeCountDown, taskType);
end
function TaskItem.OnDestroy()
    this.cfgList = nil
end

--0:未完成，1：完成未领，2：完成已领
function TaskItem.GetRewardState(id)
    local data = logicMgr.ActiveManager.GetTurntableTaskData(this.activityId,id)
    if data and data.recvStatus == 1 then
        return 2
    end
    local cfg = dataTable.getDataTableByStringId("t_periodtask",id)
    if cfg.f_TurntableTask == SPECIAL_RECHARGE_TYPE then--每充值30元可获得
        local info = ActiveManager.GetTurntableActivityInfo(this.activityId)
        if info then
            local count = math.floor(info.totalPay / cfg.f_Progress)
            if count > info.recvCount then
                return 1
            end
        end
    end
    if(data and data.process >= cfg.f_Progress) then
        return 1
    end
    return 0
end
function TaskItem.Sort()
    local stateList = {}
    for k,v in pairs(this.cfgList) do
        stateList[v.f_ActTaskId] = this.GetRewardState(v.f_ActTaskId)
    end
    table.sort(this.cfgList,function(a,b)
        local aState = stateList[a.f_ActTaskId]
        local bState = stateList[b.f_ActTaskId]
        local aw = aState == 1 and 10 or 1
        local bw = bState == 1 and 10 or 1
        if aw ~= bw then
            return aw > bw
        end
        aw = aState == 0 and 10 or 1
        bw = bState == 0 and 10 or 1
        if aw ~= bw then
            return aw > bw
        end
        return a.f_ActTaskId < b.f_ActTaskId
    end)
end
function TaskItem.updateData(timeCountDown, taskType)
    local cfgs = dataTable.getDataTable("t_periodtask")
    local funcType = taskType == 1 and 8 or 7;
    this.cfgList = {}
    for k,v in pairs(cfgs) do
        if v.f_ActivityId == this.activityId and (v.f_TurntableTask == 2 or
                v.f_TurntableTask == SPECIAL_RECHARGE_TYPE) and funcType == v.f_FunctionType then
            if funcType == 8 or (funcType == 7 and v.f_TurntableTask == 2) then
                table.insert(this.cfgList,v)
            end
        end
    end

    this.challengeTimeCountDown = timeCountDown or 1;
    this.Sort()
    this.ranklist.numItems = #this.cfgList
    this.isClick = false;
end

function TaskItem.updateItem(index, itemObj)
    index = index + 1
    local itemList = itemObj:GetChild("itemList")
    local statusCtrl = itemObj:GetController("status");
    local title = itemObj:GetChild("title")
    local btnGo = itemObj:GetChild("btn_Go");
    local btnGet = itemObj:GetChild("btn_Get");
    local effect = itemObj:GetChild("effect_tzsl")
    local cfg = this.cfgList[index]
    logicMgr.CostManager.RefreshItemList(itemList, cfg.f_Award);
    local count = 0
    if cfg.f_TurntableTask == SPECIAL_RECHARGE_TYPE then
        local info = ActiveManager.GetTurntableActivityInfo(this.activityId)
        if info then
            count = math.floor(info.totalPay / cfg.f_Progress)
            count = math.max(count - info.recvCount,0)
        end
    else
        local data = logicMgr.ActiveManager.GetTurntableTaskData(this.activityId,cfg.f_ActTaskId)
        if data ~= nil then
            count = data.process;
        end
    end
    title.text = string.format(getLanguage(cfg.f_ActTaskDesc),count);
    local state = this.GetRewardState(cfg.f_ActTaskId)
    statusCtrl.selectedIndex = state
    btnGo.onClick:Clear();
    btnGet.touchable = true;
    btnGet.grayed = false;
    btnGet.onClick:Clear();
    if state == 0 then --未完成
        btnGo.visible = cfg.f_Switchover ~= 0
        effect.visible = cfg.f_Switchover ~= 0
        if cfg.f_Switchover > 0 then
            btnGo.onClick:Add(function()
                if this.isClick then
                    return;
                end
                this.isClick = true
                this.DelayEnableClick()
                closeUI(PanelNames.UIActivityLuckyGuy)
                closeUI(PanelNames.UIActivity)
                showUI(PanelNames.UIHeadMenu);
                logicMgr.FuncOpenManager.jump(cfg.f_Switchover)
            end)
        else
            statusCtrl.selectedIndex = 1;
            btnGet.touchable = false;
            btnGet.grayed = true;
        end
        if this.challengeTimeCountDown < 0 then
            statusCtrl.selectedIndex = 3;
        end
    elseif state == 1 then--未领取
        btnGet.onClick:Add(function()
            if this.isClick then
                return
            end
            this.isClick = true
            this.DelayEnableClick()
            if cfg.f_TurntableTask == SPECIAL_RECHARGE_TYPE then
                ActivityHandler.sendRecvTurntablePayAwardMessage(this.activityId)
            else
                ActivityHandler.sendReqRecvTurntableTaskAwardMessage(this.activityId,cfg.f_ActTaskId)
            end

        end)
    end
end
function TaskItem.DelayEnableClick()
    utimer.delayCall(this.EnableClick,1000)
end
function TaskItem.EnableClick()
    this.isClick = false
end
return TaskItem;